home *** CD-ROM | disk | FTP | other *** search
/ Nothing but Tetris / Nothing but Tetris.iso / amiga / wbtris_1.3 / source / hiscore.c.pp / hiscore.c
C/C++ Source or Header  |  2000-01-01  |  4KB  |  204 lines

  1. #include "WBTRIS.h"
  2.  
  3. struct HiscorePart {
  4.    int  Position;
  5.    char Name[40];
  6.    int  Score;
  7.    int  Rows;
  8. };
  9.  
  10. struct HiscorePart Hiscore[10];
  11. extern struct TextAttr topaz8;
  12.  
  13. void HiscoreList(char *Name, int Score, int Rows, int XOffset, int YOffset)
  14. {
  15.    struct RastPort *rp;
  16.    struct Window *window;
  17.    extern struct Screen *myscreen;
  18.  
  19.    if (window = OpenWindowTags(NULL,
  20.                                 WA_Left,       XOffset,
  21.                                 WA_Top,        YOffset+(myscreen->Font->ta_YSize)+3,
  22.                                 WA_Width,      MY_WIN_WIDTH,
  23.                                 WA_Height,     MY_WIN_HEIGHT,
  24.                                 WA_Title,      "<-- Click to close!",
  25.                                 WA_Flags,      WFLG_CLOSEGADGET | WFLG_ACTIVATE | WFLG_DRAGBAR | WFLG_DEPTHGADGET,
  26.                                 WA_IDCMP,      IDCMP_CLOSEWINDOW,
  27.                                 TAG_END))
  28.       {
  29.       rp = window->RPort;
  30.  
  31.       LoadFile();
  32.       UpdateHiscore(Name, Score, Rows);
  33.       OutHiscoreList(rp);
  34.       if (SaveFile() == FALSE)
  35.          CloseWindow(window);
  36.  
  37.       WaitPort(window->UserPort);
  38.       CloseWindow(window);
  39.    }
  40. }
  41.  
  42.  
  43.  
  44. /*
  45. ** Ausgabe der Liste ins Fenster
  46. */
  47. void OutHiscoreList(struct RastPort *rp)
  48. {
  49.    int i;
  50.    char s[80];
  51.    struct IntuiText Zeile;
  52.  
  53.    Zeile.FrontPen = 1;
  54.    Zeile.BackPen = 0;
  55.    Zeile.DrawMode = JAM2;
  56.    Zeile.LeftEdge = 0;
  57.    Zeile.TopEdge = 0;
  58.    Zeile.ITextFont = &topaz8;
  59.    Zeile.NextText = NULL;
  60.  
  61.    strcpy(s, "Pos. Name                    Score Rows");
  62.    Zeile.IText = s;
  63.    PrintIText(rp, &Zeile, 12, 18);
  64.  
  65.    SetAPen(rp, 1);
  66.    Move(rp, 10, 30);
  67.    Draw(rp, MY_WIN_WIDTH - 10, 30);
  68.  
  69.    for (i=0; i<10; i++) {
  70.       sprintf(s, "%2d   %-23s%6d%5d", i+1, Hiscore[i].Name, Hiscore[i].Score, Hiscore[i].Rows);
  71.       Zeile.IText = s;
  72.       PrintIText(rp, &Zeile, 12, 36+11*i);
  73.    }
  74. }
  75.  
  76.  
  77.  
  78. /*
  79. ** Sortiert Liste neu
  80. */
  81. void UpdateHiscore(char *Name, int Score, int Rows)
  82. {
  83.    int i = 0;
  84.    int j;
  85.    BOOL equal = FALSE;
  86.  
  87.    while ((equal == FALSE) && (Score <= Hiscore[i].Score) && (i <= 9)) {
  88.       if (Score == Hiscore[i].Score)
  89.          equal = TRUE;
  90.       else
  91.          i++;
  92.    }
  93.  
  94.    if (equal == TRUE) {
  95.       while ((Score == Hiscore[i].Score) && (Rows <= Hiscore[i].Rows) && (i<=9))
  96.          i++;
  97.    }
  98.  
  99.    for (j = 9; j > i; j--) {
  100.       Hiscore[j].Score = Hiscore[j-1].Score;
  101.       strcpy(Hiscore[j].Name, Hiscore[j-1].Name);
  102.       Hiscore[j].Rows  = Hiscore[j-1].Rows;
  103.    }
  104.  
  105.    Hiscore[i].Score = Score;
  106.    strcpy(Hiscore[i].Name, Name);
  107.    Hiscore[i].Rows  = Rows;
  108. }
  109.  
  110.  
  111.  
  112. /*
  113. ** Speichert das Hiscorefile ab
  114. */
  115. BOOL SaveFile(void)
  116. {
  117.    int i;
  118.    FILE *fp;
  119.    char FName[80];
  120.  
  121.    if (getenv("WBTRIS"))
  122.       strcpy(FName, getenv("WBTRIS"));
  123.    else
  124.       strcpy(FName, FILENAME);
  125.  
  126.    if ((fp = fopen(FName, "w")) == NULL) {
  127.       fprintf(stderr, "Couldn't open file '%s'.\n", FName);
  128.       return(FALSE);
  129.    }
  130.    for (i=0; i<10; i++) {
  131.       if (strlen(Hiscore[i].Name) == 0)
  132.          strcpy(Hiscore[i].Name, "Nobody");
  133.       fprintf(fp, "%d/%s/%d/%d\n", i+1, Hiscore[i].Name, Hiscore[i].Score, Hiscore[i].Rows);
  134.    }
  135.    fclose(fp);
  136.    return(TRUE);
  137. }
  138.  
  139.  
  140.  
  141. /*
  142. ** Laedt das Hiscorefile ein
  143. */
  144. void LoadFile(void)
  145. {
  146.    int i;
  147.    FILE *fp;
  148.    int c;
  149.    char text[30];
  150.    char *ptr;
  151.    char FName[80];
  152.  
  153.    if (getenv("WBTRIS"))
  154.       strcpy(FName, getenv("WBTRIS"));
  155.    else
  156.       strcpy(FName, FILENAME);
  157.  
  158.    if ((fp = fopen(FName, "r")) == NULL) {
  159.       for (i=0; i<10; i++) {
  160.          Hiscore[i].Position = i+1;
  161.          strcpy(Hiscore[i].Name, "...");
  162.          Hiscore[i].Score = 0;
  163.          Hiscore[i].Rows = 0;
  164.       }
  165.    } else {
  166.       i = 0;
  167.       while ((c = fgetc(fp)) != EOF) {
  168.          while (c != '/') {
  169.             c = fgetc(fp);
  170.          }
  171.          Hiscore[i].Position = i+1;
  172.  
  173.          ptr = &text[0];
  174.          *ptr = '\0';
  175.          while ((c = fgetc(fp)) != '/') {
  176.             *ptr = c;
  177.             ptr++;
  178.          }
  179.          *ptr = '\0';
  180.          strcpy(Hiscore[i].Name, text);
  181.  
  182.          ptr = &text[0];
  183.          *ptr = '\0';
  184.          while ((c = fgetc(fp)) != '/') {
  185.             *ptr = c;
  186.             ptr++;
  187.          }
  188.          *ptr = '\0';
  189.          Hiscore[i].Score = atoi(text);
  190.  
  191.          ptr = &text[0];
  192.          *ptr = '\0';
  193.          while ((c = fgetc(fp)) != '\n') {
  194.             *ptr = c;
  195.             ptr++;
  196.          }
  197.          *ptr = '\0';
  198.          Hiscore[i].Rows = atoi(text);
  199.          i++;
  200.       }
  201.       fclose(fp);
  202.    }
  203. }
  204.